package web.config;

import web.common.BaseModifier;
import web.common.CodeEngineConfig;
import web.common.XmlAccessUtil;
import org.dom4j.Element;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: duanbin
 * Date: 2007-8-22
 * Time: 11:11:07
 * To change this template use File | Settings | File Templates.
 */
public class ServletBeanModifier extends BaseModifier {
    public String getFileName() {
        return CodeEngineConfig.getSpringServletFile();
    }

    public void doModify(String name) {
        //step 1. Add controller definitions
        //NOTES: hardcode using 'ns' as default namespace
        String controller = name + "Controller";
        Element targetNode = getTargetNode(controller);
        targetNode.attribute("class").setValue(
                CodeEngineConfig.getControllerPackage() + "." + controller);

        //dependency injection of refered managers
        List<String> managers = CodeEngineConfig.getControllerManagers(name);
        if(managers!=null){
            for (String manager : managers) {
                getSubPropNode(targetNode,manager,manager);
            }
        }
        //step 2. Add url mapping definitions
        //NOTES: hardcode using 'ns' as default namespace
        Element currElement = (Element) getXPath4DefaultNS("//ns:bean[@id='urlMapping']/ns:property/ns:props").selectSingleNode(doc);
        String distKey = "/" + name + "/*.v";
        getSubNode(currElement,distKey).setText(uncapFirst(controller)); 

        //step 3.[optional] Add tiles definitions if using tiles
        if(CodeEngineConfig.LAYOUT_COMPONENT_TILES.equalsIgnoreCase(
                     CodeEngineConfig.getSpringLayoutComponent())){
            //NOTES: hardcode using 'ns' as default namespace
            Element tilesElement = (Element) getXPath4DefaultNS("//ns:bean[@id='tilesConfigurer']/ns:property[@name='definitions']/ns:list").selectSingleNode(doc);
            String tilesFileName = "/WEB-INF/defs/"+uncapFirst(name)+".xml";
            getSubNode2(tilesElement,tilesFileName).setText(tilesFileName);
        }//else using sitemesh  todo
    }

    private Element getTargetNode(String dist){
        Element distNode = (Element) getXPath4DefaultNS("//ns:bean[@id='"+ uncapFirst(dist) +"']").selectSingleNode(doc);
        if(distNode==null){
            Element commonNode = (Element) getXPath4DefaultNS("//ns:bean[@id='commonMgrController']").selectSingleNode(doc);
            distNode = commonNode.createCopy();
            XmlAccessUtil.insertElementAfter(commonNode, distNode);
            distNode.attribute("id").setValue(uncapFirst(dist));
        }
        return distNode;
    }

    private Element getSubPropNode(Element parent, String distKey, String ref){
        Element sub = (Element) getXPath4DefaultNS("//ns:property[@name='"+distKey+"']").selectSingleNode(parent);
        if(sub == null){
             sub = parent.addElement("property");
             sub.addAttribute("name", distKey);
             sub.addAttribute("ref", ref);
        }else{
            sub.attribute("ref").setValue(ref);
        }
        return sub;
    }
    private Element getSubNode(Element parent, String distKey){
        Element sub = (Element)getXPath4DefaultNS("//ns:prop[@key='"+distKey+"']").selectSingleNode(parent);
        if(sub == null){
             sub = parent.addElement("prop");
             sub.addAttribute("key", distKey);
        }
        return sub;
    }
    private Element getSubNode2(Element parent, String distObj){
        Element sub = (Element)getXPath4DefaultNS("//ns:value[text()='"+distObj+"']").selectSingleNode(parent);
        if(sub == null){
             sub = parent.addElement("value");
        }
        return sub;
    }
}
